//
//  FortyViewController.m
//  Depressed.domo
//
//  Created by rimi on 16/5/27.
//  Copyright © 2016年 薛涛. All rights reserved.
//

#import "FortyViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface FortyViewController ()<AVAudioPlayerDelegate>
{
    BOOL playing; /**记录播放状态*/
    NSArray *musicNames; /**音乐名*/
    NSInteger currentMusicIndex; /**当前播放音乐下标*/

}

@property (strong ,nonatomic) AVAudioPlayer *audioPlayer;/**声明播放器*/
@property (strong ,nonatomic) UIButton *btn;
@property (strong ,nonatomic) UIButton *btn1;
@property (strong ,nonatomic) UIButton *btn2;
- (void)initaializeDudioPlayerWithMusicName:(NSString *)musicName shouldAutoPlay:(BOOL)autoPlay;

@end

@implementation FortyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"音乐播放器";
    self.view.backgroundColor = [UIColor whiteColor];
    [self initUI];
    playing = NO;
    /**根据bundle 路径同一格式文件初始化数组*/
    musicNames = [NSArray arrayWithArray:[[NSBundle mainBundle]pathsForResourcesOfType:@"mp3" inDirectory:nil]];
    NSLog(@"%@",musicNames);
}

- (void) initaializeDudioPlayerWithMusicName:(NSString *)musicName shouldAutoPlay:(BOOL)autoPlay {
    /**获取音频资源地址*/
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForAuxiliaryExecutable:musicName]];
    /**初始化音频播放器*/
    NSError *error = nil;/**错误信息类*/
    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    if (error) {
        /**准备播放*/
        [self.audioPlayer prepareToPlay];
        /**设置代理*/
        self.audioPlayer.delegate = self;
        /**设置循环模式
         -1  无限循环
         0   正常播放
         1   循环播放一次
         2   循环播放两次
         依次类推
         */
        self.audioPlayer.numberOfLoops = 0;
        /**设置音量 0.0 - 1.0*/
        self.audioPlayer.volume = 1.0;
    }
    if (autoPlay) {
        /**播放*/
        [self.audioPlayer play];
    }
}

#pragma mark UIbutton
- (void)button:(UIButton *)sender {
    currentMusicIndex = currentMusicIndex == 0 ? musicNames.count - 1 : - - currentMusicIndex;
    [self initaializeDudioPlayerWithMusicName:[musicNames[currentMusicIndex]lastPathComponent] shouldAutoPlay:playing];
    NSLog(@"12");
}

- (void)button1:(UIButton *)sender {
    if (!self.audioPlayer.playing) {
        [self.audioPlayer play];
    }else {
        [self.audioPlayer pause];
    }
    playing = !playing;
    sender.selected = playing;
    NSLog(@"13");
}

- (void)button2:(UIButton *)sender {
    currentMusicIndex = currentMusicIndex == musicNames.count - 1 ? 0 : ++ currentMusicIndex;
    [self initaializeDudioPlayerWithMusicName:[musicNames[currentMusicIndex]lastPathComponent] shouldAutoPlay:playing];
    NSLog(@"14");
}
#pragma mark setter
- (void)setAudioPlayer:(AVAudioPlayer *)audioPlayer {
    if ([audioPlayer isPlaying]) {
        [audioPlayer stop];
    }
    audioPlayer = audioPlayer;
}

#pragma mark *** AVAudioPlayerDelegate ***
/**播放完一首歌的时候触发*/
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
   /**执行下一曲*/
    [self btn2];
}
/**界面*/
- (void)initUI {
    _btn = [UIButton buttonWithType:UIButtonTypeSystem];
    _btn.frame = CRM(50, 400, 50, 50);
    [_btn setImage:[UIImage imageNamed:@"iconfont-kuaitui.png"] forState:UIControlStateNormal];
    [_btn addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    _btn.tag = 100;
    [self.view addSubview:_btn];
    
    _btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
    _btn1.frame = CRM(100, 400, 50, 50);
    [_btn1 setImage:[UIImage imageNamed:@"iconfont-zanting.png"] forState:UIControlStateNormal];
    
    
    _btn1.tag = 101;
    [_btn1 addTarget:self action:@selector(button1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btn1];
    
    _btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
    _btn2.frame = CRM(150, 400, 50, 50);
    [_btn2 setImage:[UIImage imageNamed:@"iconfont-kuaitui.png"] forState:UIControlStateNormal];
    _btn2.tag = 102;
    [_btn2 addTarget:self action:@selector(button2:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_btn2];
}
@end
